package com.qiguliuxing.dts.db.service;

import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.db.dao.IoralRawMapper;
import com.qiguliuxing.dts.db.domain.IoralRaw;
import com.qiguliuxing.dts.db.domain.IoralRaw.Column;
import com.qiguliuxing.dts.db.domain.IoralRawExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
@Service
public class IoralRawService {
    private static final Logger logger = LoggerFactory.getLogger(IoralRawService.class);
    Column[] columns = new Column[] { Column.id, Column.name, Column.productionAddress,Column.dispatchActivity,Column.calibrationActivity, Column.batchNumber, Column.unit, Column.supplier, Column.totalActivity, Column.purchaseDate, Column.notes, Column.pieces,Column.isEnd,Column.remainingActivity,Column.gaolongId,Column.solutionId};
    @Resource
    private IoralRawMapper ioralRawMapper;


    /**
     * 获取热卖商品
     *
     * @param offset
     * @param limit
     * @return
     */
    public List<IoralRaw> queryById(int offset, int limit) {
        IoralRawExample example = new IoralRawExample();
        /*        example.or().andIseffectiveEqualTo(true);*/
        example.setOrderByClause("overdue desc");
        PageHelper.startPage(offset, limit);

        return ioralRawMapper.selectByExampleSelective(example, columns);
    }

    /**
     * 获取新品上市
     *
     * @param offset
     * @param limit
     * @return
     */
    public List<IoralRaw> queryByNew(int offset, int limit) {
        IoralRawExample example = new IoralRawExample();
        example.setOrderByClause("purchase_date desc");
        PageHelper.startPage(offset, limit);

        return ioralRawMapper.selectByExampleSelective(example, columns);
    }

    /**
     * 获取分类下的商品
     *
     * @param catList
     * @param offset
     * @param limit
     * @return
     */
    public List<IoralRaw> queryByid(List<Integer> catList, int offset, int limit) {
        IoralRawExample example = new IoralRawExample();
        /*   example.or().andIdIn(catList).andIseffectiveEqualTo(true);*/
        example.setOrderByClause("date  desc");
        PageHelper.startPage(offset, limit);

        return ioralRawMapper.selectByExampleSelective(example, columns);
    }


    /*开发一个起始日期-结束日期查询功能*/
    public List<IoralRaw> querySelective(String startTime,String endTime, Integer page, Integer limit, String sort,
                                      String order) {
        IoralRawExample example = new IoralRawExample();
        IoralRawExample.Criteria criteria = example.createCriteria();

        if (!startTime.isEmpty() && !endTime.isEmpty()){
            criteria.andPurchaseDateBetween(LocalDate.parse(startTime),LocalDate.parse(endTime));
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return ioralRawMapper.selectByExample(example);
    }
    public List<IoralRaw> querySelective2(Integer page, Integer limit, String sort,
                                         String order) {
        IoralRawExample example = new IoralRawExample();
        IoralRawExample.Criteria criteria = example.createCriteria();


       /* if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }*/

        PageHelper.startPage(page, limit);
        return ioralRawMapper.selectByExample(example);
    }
 //查询可使用的原料列表
    public List<IoralRaw> queryNew() {
        IoralRawExample example = new IoralRawExample();
        IoralRawExample.Criteria criteria = example.createCriteria();
        criteria.andIsEndEqualTo(false);
        return ioralRawMapper.selectByExample(example);
    }


    /**
     * 获取某个文档,包含完整信息
     *
     * @param id
     * @return
     */
    public IoralRaw findById(Integer id) {
        IoralRawExample example = new IoralRawExample();
        /*example.or().andidEqualTo(id).andDeletedEqualTo(false);*/
        example.or().andIdEqualTo(id);
        /* return goodsMapper.selectOneByExampleWithBLOBs(example);*/
        return ioralRawMapper.selectOneByExampleSelective(example);
    }



    public int updateById(IoralRaw ioralRaw) {
        /*   ioralRaw.setUpdatetime(LocalDate.now());*/
        return ioralRawMapper.updateByPrimaryKeySelective(ioralRaw);
    }

    public void deleteById(Integer id) {
        /*ioralRawMapper.logicalDeleteByPrimaryKey(id);*/
    }

    public void add(IoralRaw ioralRaw) {
        /* ioralRaw.setAddTime(""LocalDate.now()"");*/
        /*ioralRaw.setUpdateTime(LocalDateTime.now());*/
        ioralRawMapper.insertSelective(ioralRaw);
    }

    /**
     * 获取所有物品总数，包括在售的和下架的，但是不包括已删除的商品
     * 获取所有文档总数。
     * @return
     */
    public int count() {
        IoralRawExample example = new IoralRawExample();
        example.or();
        return (int) ioralRawMapper.countByExample(example);
    }



    public boolean checkExistByName(String name) {
        IoralRawExample example = new IoralRawExample();
        /* example.or().andioralRawNameEqualTo(name);*/
        return ioralRawMapper.countByExample(example) != 0;
    }



}
